package com.jcfk.eam.service.base;

import com.jcfk.eam.dao.base.EamTeamDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.domain.dto.base.EamTeamDTO;
import com.jcfk.eam.domain.dto.base.EamTeamMemberDTO;
import com.jcfk.eam.domain.po.base.EamTeam;
import com.jcfk.eam.domain.query.base.EamTeamQuery;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 运维班组
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-08-14
 */
@Service
public class EamTeamService extends BaseService<EamTeamDao, EamTeam> {


    @Autowired
    private EamTeamMemberService eamTeamMemberService;

    /**
     * EamTeam分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamTeamDTO> queryByPage(DataPage<EamTeamDTO> page, EamTeamQuery query) {
        // 分页查询
        query.setTeamName(StringUtils.isEmpty(query.getTeamName()) ? query.getTeamName() : query.getTeamName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamTeam是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamTeamDTO dto) {
        QueryWrapper<EamTeam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("team_code", dto.getTeamCode());
        if (!StringUtils.isEmpty(dto.getId())) queryWrapper.ne("id", dto.getId());
        Integer count = this.count(queryWrapper);

        if (count > 0) return RetResult.fail("班组编号已存在");

        return RetResult.ok();
    }

    /**
     * 插入运维班组
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamTeamDTO dto) {

        // 保存
        EamTeam item = new EamTeam();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
        eamTeamMemberService.saveBatch(dto.getDetails(), dto.getId());
    }

    /**
     * 更新运维班组
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamTeamDTO dto) {

        // 保存
        EamTeam item = new EamTeam();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);

        eamTeamMemberService.saveBatch(dto.getDetails(), dto.getId());
    }

    /**
     * 根据批量主键删除运维班组(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamTeamDTO dto) {

        EamTeam item = new EamTeam();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamTeam> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamTeam item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取运维班组信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamTeamDTO get(String id) {
        // 查询数据
        EamTeam item = this.baseMapper.selectById(id);
        EamTeamDTO dto = new EamTeamDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamTeamDTO>
     */
    public List<EamTeamDTO> getList(EamTeamQuery query) {
        // 查询条件
        QueryWrapper<EamTeam> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getTeamCode()), EamTeam::getTeamCode, query.getTeamCode());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getTeamName()), EamTeam::getTeamName, query.getTeamName());
        qw.lambda().eq(query.getTeamType() != null, EamTeam::getTeamType, query.getTeamType());
        qw.lambda().eq(query.getEnabled() != null, EamTeam::getEnabled, query.getEnabled());
        qw.lambda().eq(query.getDeleted() != null, EamTeam::getDeleted, query.getDeleted());

        // 查询数据
        List<EamTeam> list = super.list(qw);

        return convertList(list, EamTeamDTO.class);
    }

    /**
     * 根据主键获取运维班组信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamTeam item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }

    /**
     * 根据员工号获取班组
     * memberEmpNo
     * @return
     */
    public List<EamTeamDTO> getListByEmpNo(String memberEmpNo){
        List<EamTeamDTO> list=this.baseMapper.getListByEmpNo(memberEmpNo);
        return list;
    }
}

